import {
  ImageUrl
} from '../../../../../utils/imageHelper';
const reqInterface = require("../../../../../api/models");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ImageObj: ImageUrl,
    intention: {
      level1: 0,
      level2: 0,
      level3: 0,
      level4: 0
    },
    showArea: false,
    // 省市区
    originAddressOptionNames: {
      text: 'name',
      value: 'id',
      children: 'childs'
    },
    originAddressOption: [],

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getOriginAddressData(10000);
  },
  openPopup(e) {
    let self = this;
    switch (e.currentTarget.dataset.name) {
      case 'area':
        self.setData({
          showArea: true
        })
        break;
    }
  },

  getOriginAddressData(val) {
    let self = this;
    reqInterface.GetDictDetail({ id: val }).then(res => {
      console.log(res.childs)
      self.setData({
        pro: res.childs,
        originAddressOption: res.childs
      })
    })
  },

  originAddress() {
    this.setData({
      showOriginAddress: true
    })
  },

  originAddressChange(e) {
    let self = this;
    switch (e.detail.selectedOptions.length) {
      case 1:
        reqInterface.GetDictDetail({ id: e.detail.value }).then(res => {
          let idx1 = self.findidx(self.data.originAddressOption, e.detail.value);
          self.data.originAddressOption[idx1].childs = res.childs;
          self.setData({
            originAddressOption: self.data.originAddressOption
          })
        })
        break;
      case 2:
        reqInterface.GetDictDetail({ id: e.detail.value }).then(res => {
          let idx1 = self.findidx(self.data.originAddressOption, e.detail.selectedOptions[0].id);
          let idx2 = self.findidx(self.data.originAddressOption[idx1].childs, e.detail.value);
          console.log(idx1, idx2)
          self.data.originAddressOption[idx1].childs[idx2].childs = res.childs;
          self.setData({
            originAddressOption: self.data.originAddressOption
          })
        })
        break;
      case 3:
        reqInterface.GetDictDetail({ id: e.detail.value }).then(res => {
          let idx1 = self.findidx(self.data.originAddressOption, e.detail.selectedOptions[0].id);
          let idx2 = self.findidx(self.data.originAddressOption[idx1].childs, e.detail.selectedOptions[1].id);
          let idx3 = self.findidx(self.data.originAddressOption[idx1].childs[idx2].childs, e.detail.value);
          console.log(idx1, idx2)
          self.data.originAddressOption[idx1].childs[idx2].childs[idx3].childs = res.childs;
          self.setData({
            originAddressOption: self.data.originAddressOption
          })
        })
        break;
      default:
        self.setData({
          'basic.reg_level4': e.detail.value,
        })
    }
  },

  originAddressSave(e) {
    let self = this;
    self.setData({
      showOriginAddress: false
    })
    console.log(self.data.basic)
  },


  findidx(arr, val) {
    return arr.findIndex(item => {
      return item.id === val
    })
  }
})